<?php

Yii::import('application.extensions.NuSoap.NSoapServerController');

class DefaultController extends NSoapServerController
{

	public function __construct($id,$module=null)
	{
		parent::__construct($id, 'DefaultController', $module);
	}

	/**
	 * Login into Harliane.
	 *
	 * @param string $username
	 * @param string $password
	 * @return HSession User information
	 * @soap
	 */
	public function login($username, $password) {
		$form = new LoginForm();
		$form->username = $username;
		$form->password = $password;
		
		if(!$form->validate()) {
			$errors = array();
			foreach ($form->errors as $field=>$error) {
				$errors[] = "$field={".join(",", $error)."}";
			}
			throw new CException(join("\n", $errors), 500);
		}
		
		$session=new CHttpSession;
		$session->open();
		$hUser = new HUser();
		$hUser->attributes = Yii::app()->user->getState('dbuser')->attributes;
		$session['user'] = $hUser;
		
		$res = new HSession();
		$res->user = $session['user'];
		$res->session_id = $session->getSessionID();
		$res->session_name = $session->getSessionName();
		
		$res->roles = array();
		$roles = Yii::app()->authManager->getAuthAssignments($username);
		
		foreach ($roles as $role) {
			$tasks = Yii::app()->authManager->getAuthItem($role->itemName)->getChildren();
			foreach($tasks as $task) {
				$res->roles[] = $task->name;
			}
		}
		
		
		return $res;
	}
	
	/**
	 * Close user session
	 * 
	 * @return boolean if session has been closed.
	 * @soap
	 */
	public function logout(){
 		$session=new CHttpSession;
 		return $session->closeSession();
	}
	
	/**
	 * Gets all available languages in Harliane.
	 * 
	 * @return HLanguage[] All available languages.
	 * @soap
	 */
	public function findAllLanguages() {
		$langs = array();
		
		foreach (Constants::userLanguages() as $code => $label) {
			$lang = new HLanguage();
			$lang->code = $code;
			$lang->label = htmlentities($label);
			$langs[] = $lang;
		}
		
		return $langs;
	}
	
	/**
	 * List all users
	 *
	 * @return HUser[] Users informations
	 * @soap
	 */
	public function findAllUsers() {
 		$session=new CHttpSession;
		if (!$session->contains('user')) {
			$session->close();
			throw new CException("You have to logged in before.", 500);
		}
 		
 		$users = User::model()->findAll();
		$hUsers = array();
		foreach ($users as $user) {
			$hUser = new HUser();
			$hUser->attributes = $user->attributes;
			$hUsers[] = $hUser;
		}
		
		return $hUsers;
	}
	
	/**
	 * Update current user account information
	 * 
	 * @param string $password
	 * @param HUser $user
	 * @throws CException
	 * 
	 * @return HSession
	 * @soap
	 */
	public function updateAccountInformations($password, $user) {
		$session=new CHttpSession;
		if (!$session->contains('user')) {
			$session->close();
			throw new CException("You have to logged in before.", 500);
		}
		
		$form = new InformationForm();
		$form->attributes = $user;
		$form->password = $password;
		
		$form->save();
		if ($form->hasErrors()) {
			$errors = array();
			foreach ($form->errors as $field=>$error) {
				$errors[] = "$field={".join(",", $error)."}";
			}
			throw new CException(join("\n", $errors), 500);
		}
		
		return $this->login(Yii::app()->user->id, $password);
	}
	
	/**
	 * Update current user password
	 * 
	 * @param string $password
	 * @param string $password_new
	 * @param string $password_check
	 * 
	 * @return HSession
	 * @soap
	 */
	public function changePassword ($password, $password_new, $password_check) {
		$session=new CHttpSession;
		if (!$session->contains('user')) {
			$session->close();
			throw new CException("You have to logged in before.", 500);
		}
		
		$form = new PasswordForm();
		$form->password = $password;
		$form->password_new = $password_new;
		$form->password_check = $password_check;
		
		$form->save();
		if ($form->hasErrors()) {
			$errors = array();
			foreach ($form->errors as $field=>$error) {
				$errors[] = "$field={".join(",", $error)."}";
			}
			throw new CException(join("\n", $errors), 500);
		}
		
		return $this->login(Yii::app()->user->id, $password_new);
	}

}